
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" name="viewport" 
         content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=yes, target-densitydpi=medium-dpi"/>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" />
    <script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js"></script>
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<title>jQueryMobile</title>
<script type="text/javascript">
	var geocoder;
	var map;
	var infowindow = new google.maps.InfoWindow();
	var marker;

	window.onload = function(){
		//geolocation을 지원하는 여부 확인
		if(navigator.geolocation==undefined){
			alert("위치 정보를 이용할 수 없습니다."); return;
		}
		//감시시작
		if(navigator.geolocation){
			navigator.geolocation.getCurrentPosition(sucessCallback, errorCallback);
		}else{
			alert("브라우저가 위치정보를 제공하지 않습니다.");
		}
	};

	//성공했을때
	function sucessCallback(position){
		geocoder = new google.maps.Geocoder();	
		var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
		var myOptions = {
		  zoom: 16,
		  center: latlng,
		  mapTypeId: google.maps.MapTypeId.ROADMAP
		};

		var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
		// 초기 마커 위치잡기!
		var marker = new google.maps.Marker(); 
		var infowindow = new google.maps.InfoWindow();
		
		
			if (geocoder) {
				geocoder.geocode({'latLng': latlng}, function(results, status) {
				  if (status == google.maps.GeocoderStatus.OK) {
					if (results[1]) {
					  map.setZoom(16);
					  marker.setPosition(latlng);
					  marker.setMap(map);
					  infowindow.setContent(results[0].formatted_address);
					  infowindow.open(map, marker);
					  
					  // 현위치 전송 버튼 누를시
					  // 현위치의 주소값을 안드로이드로 넘긴다.

					}
				  } else {
					alert("지오코드 실패!: " + status);
				  }
				});
			  }
			  
		$('#btSubmit').bind('click',function(){
			window.android.sendToAndroid(infowindow.getContent());
		});
					  
		google.maps.event.addListener(map,'click',function(event){
			var userClickPosition = new google.maps.LatLng(event.latLng.lat(),event.latLng.lng());
			if (geocoder) {
				geocoder.geocode({'latLng': userClickPosition}, function(results, status) {
				  if (status == google.maps.GeocoderStatus.OK) {
					if (results[1]) {
					  map.setZoom(16);
					  marker.setPosition(userClickPosition);
					  marker.setMap(map);
					  infowindow.setContent(results[0].formatted_address);
					  infowindow.open(map, marker);
					}
				  } else {
					alert("지오코드 실패!: " + status);
				  }
				});
			  }
		});
	}

	//실패했을때 
	 function errorCallback(err){
	 	alert("실패("+err.code+")"+err.message);
	 	geocoder = new google.maps.Geocoder();
		var latlng = new google.maps.LatLng(37.52,126.92);
		var myOptions = {
		  zoom: 16,
		  center: latlng,
		  mapTypeId: google.maps.MapTypeId.ROADMAP
		};

		var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
		// 초기 마커 위치잡기!
		var marker = new google.maps.Marker({map: map, position: map.getCenter()}); 
		var infowindow = new google.maps.InfoWindow();
	 }

</script>
</head>
<body>

<div data-role="page">
    <div data-role="header">
        <h2>현재 위치 설정</h2>
    </div>
    <p>
    <div id="map_canvas" style="width: 310px; height: 400px; margin: 0 auto; border:1px solid black;"/>
      
</div>

<div data-role="controlgroup" data-type="horizontal">
  <p align="center">
  <input type="button" value="주소전송" id="btSubmit" />
  </p>
</div>

</body>
</html>


